Libraries

The following libraries were used:

library(tidyverse)
library(here)
library(plotly)
library(htmlwidgets)
library(lubridate)

Adding size data

load(file = here("data/data_tidy.RData")) # load object data_tidy

BodyLengths <- tibble(Species = c("Ceriodaphnia sp.", "C. megalops", "D. cucullata", "D. curvirostris", "D. hyalina var. gellata", "D. hyalina var. lacustris", "D. hyalina", "D. longispina", "D. pulex", "D. magna", "Calanoid copepods", "Cyclops", "Bosmina coregoni", "B. longirostris", "Sida sp.", "S. crystallina", "Chydorus ovalis", "Eurycercus lamellatus", "Alona spp.", "A. quadrangularis", "Asplanchna", "Keratella spp.", "Ostracod", "Diaptomus"),
                      BodyLength = c(0.4636, 0.925, 0.8593, 1.58, 0.9064, 0.9064, 0.9064, 1.0347, 1.1656, 1.4214, 0.6860, 1.0369, 0.4230, 0.3637, 0.5075, 0.5075, 0.475, 1.975, 0.5235, 0.9679, 0.4747, 0.3495, 1.25, 0.9886)) # species and their body sizes

data <- left_join(data_tidy, BodyLengths, by = "Species") # add species body size to the dataframe

data <- mutate(data, SizeClass = case_when(BodyLength <= 0.6 ~ "Small (<= 0.6 mm)",
                                           BodyLength <= 1.0 ~ "Medium ( 0.6 < x <= 1.0 mm)",
                                           BodyLength > 1.0 ~ "Large (> 1.0 mm)")) # define different categories of body sizes

Data wrangling and creating plots

Using nested for loops, R cycles through all six lakes and year by year. It then creates plots of all possible combinations.

lakes <- unique(data_tidy$LakeName) # extract the names of the lakes
years <- unique(data_tidy$Year) # extract the years where counts were measured

plotlist <- list() # create an empty list to add to

for (i in 1:length(lakes)){
  Ldata <- filter(data, LakeName == lakes[i])
  
  for (j in 1:length(years)){
    Ydata <- filter(Ldata, Year == years[j])
    
    if (nrow(Ydata) > 0) {
      
      Xdata <- Ydata %>% mutate("Abundance_ind/L" = (Counts / Proportion_of_sample_counted) / Sampling_volume_net_L,
                                "Relative_abundance" = Counts / Total_individuals * 100)
      
      summary <- Xdata %>% group_by(Species, SizeClass, Date) %>% summarize(sum_counts = sum(Counts, na.rm = TRUE),
                                                                             sum_total = sum(Total_individuals, na.rm = TRUE)) # Calculate counts per Date
      
      summary <- summary %>% group_by(SizeClass, Date, sum_total) %>% summarize(sum_counts = sum(sum_counts, na.rm = TRUE))
      
      summary <- summary %>% mutate("Relative_abundance" = sum_counts / sum_total * 100) # Calculate relative abundance per Date
      result <- summary %>% ggplot(aes(x = factor(Date), y = Relative_abundance, fill = SizeClass))+
        geom_col(color = "Black", linewidth = 0.05)+
        labs(x = "Month", y = "Relative abundance (%)", title = paste0(lakes[[i]], " in ", years[[j]]))+
        scale_x_discrete(labels = month(summary$Date, label = TRUE))+
        theme_minimal()
      
      plotlist[[(length(plotlist)+1)]] <- result
    } else {
      warning(paste0("No data for ", lakes[[i]], " in the year ", years[[j]]))
    }
  }
}
## Warning: No data for Coneries Lake in the year 2010
## Warning: No data for Coneries Lake in the year 2011
## Warning: No data for Coneries Lake in the year 2012
## Warning: No data for Main Pond in the year 2010
## Warning: No data for Main Pond in the year 2011
## Warning: No data for Main Pond in the year 2012
## Warning: No data for Church Pond in the year 2010
## Warning: No data for Church Pond in the year 2011
## Warning: No data for Church Pond in the year 2012
## Warning: No data for Beeston Pond in the year 2010
## Warning: No data for Beeston Pond in the year 2011
## Warning: No data for Beeston Pond in the year 2012

Interactive plots

The {plotly} package was used to create interactive plots, to make it possible to see the actual values of a bar.

htmltools::tagList(lapply(1:length(plotlist), function(x) { ggplotly(plotlist[[x]]) }))

Creating plots for abundance ind/L

plotlist <- list()
lakes <- unique(data$LakeName)
years <- unique(data$Year)

for (i in 1:length(lakes)){
  Ldata <- filter(data, LakeName == lakes[i])
  
  for (j in 1:length(years)){
    Ydata <- filter(Ldata, Year == years[j])
    
    if (nrow(Ydata) > 0) {
      
      summary <- Ydata %>% group_by(SizeClass, Date) %>% summarize(CountsClass = sum(Counts, na.rm = TRUE))
      Zdata <- unique(Ydata[, c("Sampling_volume_net_L", "Proportion_of_sample_counted", "Date")])
      Xdata <- left_join(summary, Zdata, by = "Date")
      Pdata <- Xdata %>% mutate("Abundance_ind/L" = (CountsClass / Proportion_of_sample_counted) / Sampling_volume_net_L)
      
      result <- Pdata %>% ggplot(aes(x = factor(Date), y = `Abundance_ind/L`, fill = SizeClass))+
        geom_col(color = "Black", linewidth = 0.05)+
        labs(x = "Month", y = "Abundance ind/L", title = paste0(lakes[[i]], " in ", years[[j]]))+
        scale_x_discrete(labels = month(Pdata$Date, label = TRUE))+
        theme_minimal()
      
      plotlist[[(length(plotlist)+1)]] <- result
    } else {
      warning(paste0("No data for ", lakes[[i]], " in the year ", years[[j]]))
    }
  }
}
## Warning: No data for Coneries Lake in the year 2010
## Warning: No data for Coneries Lake in the year 2011
## Warning: No data for Coneries Lake in the year 2012
## Warning: No data for Main Pond in the year 2010
## Warning: No data for Main Pond in the year 2011
## Warning: No data for Main Pond in the year 2012
## Warning: No data for Church Pond in the year 2010
## Warning: No data for Church Pond in the year 2011
## Warning: No data for Church Pond in the year 2012
## Warning: No data for Beeston Pond in the year 2010
## Warning: No data for Beeston Pond in the year 2011
## Warning: No data for Beeston Pond in the year 2012
htmltools::tagList(lapply(1:length(plotlist), function(x) { ggplotly(plotlist[[x]]) }))